import { message } from "antd";
import { periodTypeData } from "../../api/DesignTableApi";
export default class ColumnEditorService {
    context;
    constructor(context) {
        this.context = context;
    }
    columnAggregationChange(e) {
        const currentColumnAggregation = e.itemData.id;
        this.context.setState({
            currentColumnAggregation,
        });
    }
    rowAggregationChange(e) {
        const currentRowAggregation = e.itemData.id;
        this.context.setState({
            currentRowAggregation,
        });
    }
    get hot() {
        return this.context.gridRef.hot;
    }
    async removeQiJianType(col) {
        this.context.gridRef.setTableContentChange(true, {
            type: "移除期间类型",
            params: {},
        });
        const headerData = this.context.headerService.headerColumn;
        const needDeleteGroupId = headerData[col].config.groupId; // 1. 根据 col 找对应的groupId = 1
        const key = headerData[col].key;
        const currentGridData = this.context.gridRef.getCpasTableGrid();
        const result = await periodTypeData({
            action: "remove",
            type: "",
            key,
            groupId: needDeleteGroupId,
            iGridData: currentGridData,
        });
        if (result.ok) {
            await this.context.gridRef.reloadTable(result.data, null, null, true);
        }
        else {
            this.context.showErrorNotify("删除期间类型失败");
        }
        this.context.hot.deselectCell();
        // // 一.添加列
        // this.context.gridService.addColumn("right", false, key);
        // // 二.删除指定列
        // //2. 把groupId = 1 的对象的key 都取出来
        // const needDeleteColumnArr = headerData.filter(
        //   (item, index) => item.config.groupId === needDeleteGroupId
        // );
        // // 3. 删除对应的key 的 列
        // for (let i = 0; i < needDeleteColumnArr.length; i++) {
        //   const needDeleteColumnKey = needDeleteColumnArr[i].key;
        //   this.context.removeColumnHandle(needDeleteColumnKey);
        // }
        // // 从新计算逆序位置信息
        // this.computerNeedReverseGroupInfo();
    }
    async createQianJianColumn(currentQiJianType) {
        this.context.gridRef.setTableContentChange(true, {
            type: "创建期间类型",
            params: {
                currentQiJianType,
            },
        });
        // 一. 请求吕哥的接口，返回数据
        // let resData = await this.getQianJianTypeResult();
        // 二. 先添加列
        const key = this.context.gridRef.state.currentRecord.key;
        const currentGridData = this.context.gridRef.getCpasTableGrid();
        // dbname, action,type, key, iGridData
        const result = await periodTypeData({
            action: "add",
            type: currentQiJianType,
            key,
            iGridData: currentGridData,
        });
        console.log(result);
        if (result.ok) {
            await this.context.gridRef.reloadTable(result.data, null, null, true);
        }
        else {
            this.context.showErrorNotify("创建期间类型失败");
        }
        // for (let i = 0; i < resData.length; i++) {
        //   if (i === 0) {
        //     this.context.gridService.addColumn("right", false, key);
        //     this.context.gridService.addColumn("right", false, key);
        //   } else {
        //     this.context.gridService.addColumn("right", false, key);
        //   }
        // }
        // // 2. 再删除选中的列
        // this.context.removeColumnHandle(key);
        // // 三.修改对应的数据
        // this.updateDataForQiJianChange(currentQiJianType, resData);
        //
        // // 从新计算逆序位置信息
        // this.computerNeedReverseGroupInfo();
    }
    // 期间类型修改
    async qiJianTypeChange(e) {
        const currentQiJianType = e.itemData.id;
        if (!this.context.gridRef.state.currentRecord) {
            this.context.showErrorNotify("请选择表头进行操作");
            return;
        }
        if (!this.context.gridRef.state.currentRecord.key) {
            this.context.showErrorNotify("请选择表头进行操作");
            return;
        }
        const col = this.context.gridRef.state.currentRecord.coords.col;
        const headerData = this.context.headerService.headerColumn;
        const canCreateQijianColumn = !headerData[col]?.config?.timeColumn;
        if (canCreateQijianColumn) {
            // 选中的列没有期间类型,才可以创建
            if (currentQiJianType !== "none") {
                await this.createQianJianColumn(currentQiJianType);
            }
        }
        else {
            if (currentQiJianType === "none") {
                await this.removeQiJianType(col);
            }
            else {
                message.info("该列已经设置期间类型,不能再重复创建");
            }
        }
        this.context.setState({
            currentQiJianType,
        });
        // this.hot.selectColumns(col); //
    }
    computerNeedReverseGroupInfo() {
        const needReverseGroupInfo = []; // [{reverseStart: 2, reverseEnd: 6}, {reverseStart: 8, reverseEnd: 12}]
        const headerData = this.context.gridRef.headerData;
        let qiJianColumnCount;
        for (let i = 0; i < headerData.length;) {
            const headerDadaElement = headerData[i];
            const timeColumn = headerDadaElement.config?.timeColumn;
            if (timeColumn) {
                // 如果是期间类型列
                qiJianColumnCount = timeColumn.items.length;
                const reverseEnd = i + qiJianColumnCount - 1;
                needReverseGroupInfo.push({
                    reverseStart: i,
                    reverseEnd,
                    elementCount: qiJianColumnCount,
                });
                i = i + qiJianColumnCount;
            }
            else {
                i++;
            }
        }
        console.log("computerNeedReverseGroupInfo");
        console.log("needReverseGroupInfo:", needReverseGroupInfo);
        this.context.needReverseGroupInfo = needReverseGroupInfo;
        return needReverseGroupInfo;
    }
}
